package com.gec.anan.driver.controller;

import com.gec.anan.common.result.Result;
import com.gec.anan.driver.config.TencentCloudProperties;
import com.gec.anan.driver.service.CosService;
import com.gec.anan.model.vo.driver.CosUploadVo;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.http.HttpProtocol;
import com.qcloud.cos.region.Region;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@Tag(name = "腾讯云cos上传接口管理")
@RestController
@RequestMapping(value="/cos")
@SuppressWarnings({"unchecked", "rawtypes"})
public class CosController {
    @Autowired
    private TencentCloudProperties tencentCloudProperties;

    @Bean
    public COSClient getPrivateCOSClient() {
        //1.初始化客户信息
        COSCredentials cred = new BasicCOSCredentials(tencentCloudProperties.getSecretId(),
                tencentCloudProperties.getSecretKey());
        //2.设置buket和cos跨域
        ClientConfig clientConfig = new ClientConfig();
        clientConfig.setRegion(new Region(tencentCloudProperties.getRegion()));
        clientConfig.setHttpProtocol(HttpProtocol.https);
        //3.生成cos客户端
        COSClient cosClient = new COSClient(cred, clientConfig);
        //4.返回cos客户端对象
        return cosClient;
    }
    @Autowired
    private CosService cosService;


    @Operation(summary = "上传")
    @PostMapping("/upload")
    public Result<CosUploadVo> uploadVoResult(@RequestPart("file")MultipartFile multipartFile,
                                              @RequestParam("path")String path){
        return Result.ok(cosService.upload(multipartFile,path));
    }

}

